package com.zretc.personcentre.service.impl;

import com.zretc.api.pojo.Participation;
import com.zretc.api.pojo.UserInfo;
import com.zretc.api.pojo.blog;
import com.zretc.api.pojo.personcentreModule.ApplidRecommend;
import com.zretc.api.pojo.personcentreModule.Points;
import com.zretc.api.pojo.recommendModule.Recommend;
import com.zretc.common.entity.Pager;
import com.zretc.common.entity.R;
import com.zretc.common.entity.RespResult;
import com.zretc.personcentre.dao.*;
import com.zretc.personcentre.service.FileServiceClient;
import com.zretc.personcentre.service.PersonCentreService;
import com.zretc.redis.service.RedisService;
import com.zretc.security.entity.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import static com.zretc.common.constant.WebConstant.ERROR_503;



@Service
  public class PersonCentreServiceImpl implements PersonCentreService {
    @Autowired
     PersonMessageMapper personMessageMapper;
    @Autowired
    ParticipationMapper participationMapper;
    @Autowired
    PublishedBlogMapper publishedBlogMapper;
    @Autowired
    ApplidRecommendMapper applidRecommendMapper;
    @Autowired
    RecommendMapper recommendMapper;
    @Autowired
    PointsMapper pointsMapper;
    @Autowired
    RedisService redisService;
    @Autowired
    private FileServiceClient fileServiceClient;


    public RespResult<UserInfo> ShowPersonMessage()
    {
        LoginUser loginUser = (LoginUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        Integer Id=loginUser.getUser().getId();
        UserInfo message = personMessageMapper.GetPersonMessageById(Id);
        return R.success(message);
    }

    @Transactional
    public RespResult<String> UpdatePersonMessage(UserInfo userInfo, MultipartFile file){
        LoginUser loginUser = (LoginUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        Integer Id = loginUser.getUser().getId();
        userInfo.setUserId(Id);
        try {
            file.getInputStream().close();
            System.gc();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        if(file!=null){
            String url = fileServiceClient.upload(file).getData().toString();
            userInfo.setAvatar(url);
            personMessageMapper.sentAvatar(Id,url);
        }
        if(personMessageMapper.UpdatePersonMessage(userInfo)){
            return R.success("修改成功");
        } else
            return R.fail(505,"修改失败");
    }
    public Map<String, Object> ShowParticipateActivity(Integer pageNum, Integer pageSize){
        LoginUser loginUser = (LoginUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        Integer Id=loginUser.getUser().getId();
        Integer count = participationMapper.getCount(Id);

        if(count == 0){
            return null;
        }
        Pager pager = new Pager(count, pageNum, pageSize);
        ArrayList<Participation> participateActivities = participationMapper.GetParticipationMessageById(pager.getStartIndex(), pager.getPageSize(),Id);
        HashMap<String,Object> map = new HashMap<>();
        map.put("pager",pager);
        map.put("list",participateActivities);
        return map;
    }
    public Map<String, Object> ShowPublishedBlog(Integer pageNum, Integer pageSize){
        LoginUser loginUser = (LoginUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        Integer Id=loginUser.getUser().getId();
        Integer count = publishedBlogMapper.getCount(Id);
        if(count == 0){
            return null;
        }
        Pager pager = new Pager(count, pageNum, pageSize);
        ArrayList<blog> PublishedBlogs = publishedBlogMapper.GetPublishedBlogMessageById(pager.getStartIndex(), pager.getPageSize(),Id);
        HashMap<String,Object> map = new HashMap<>();
        map.put("pager",pager);
        map.put("list",PublishedBlogs);
        return map;
    }

    public Map<String, Object> ShowApplidRecommend(Integer pageNum, Integer pageSize){
        LoginUser loginUser = (LoginUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        Integer Id=loginUser.getUser().getId();
        Integer count = applidRecommendMapper.getCount(Id);
        if(count == 0){
            return null;
        }
        Pager pager = new Pager(count, pageNum, pageSize);
        ArrayList<ApplidRecommend> ApplidRecommends = applidRecommendMapper.GetApplidRecommendMessageById(pager.getStartIndex(), pager.getPageSize(),Id);
        HashMap<String,Object> map = new HashMap<>();
        map.put("pager",pager);
        map.put("list",ApplidRecommends);
        return map;
    }
    public Map<String, Object> ShowRecommend(Integer pageNum, Integer pageSize){
        LoginUser loginUser = (LoginUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        Integer Id=loginUser.getUser().getId();
        Integer count = recommendMapper.getCount(Id);
        if(count == 0){
            return null;
        }
        Pager pager = new Pager(count, pageNum, pageSize);
        ArrayList<Recommend> Recommends = recommendMapper.GetRecommendMessageById(pager.getStartIndex(), pager.getPageSize(),Id);
        HashMap<String,Object> map = new HashMap<>();
        map.put("pager",pager);
        map.put("list",Recommends);
        return map;
    }
    public RespResult<String> DeleteRecommend(Integer id){
        //LoginUser loginUser = (LoginUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        if(recommendMapper.DeleteRecommendById(id)){
            return R.success("删除成功");
        }
        else
            return R.fail(503,ERROR_503);
    }
    public Map<String, Object> ShowPoints(Integer pageNum, Integer pageSize){
        LoginUser loginUser = (LoginUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        Integer Id=loginUser.getUser().getId();
        Integer count = pointsMapper.getCount(Id);
        if(count == 0){
            return null;
        }
        Pager pager = new Pager(count, pageNum, pageSize);
        ArrayList<Points> Points = pointsMapper.GetPointsMessageById(pager.getStartIndex(), pager.getPageSize(),Id);
        HashMap<String,Object> map = new HashMap<>();
        map.put("pager",pager);
        map.put("list",Points);
        return map;
    }

    @Override
    public RespResult upload(MultipartFile file) {
        try {
            file.getInputStream().close();
            System.gc();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        LoginUser loginUser = (LoginUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        Integer Id=loginUser.getUser().getId();
        if(file!=null){
            String url = fileServiceClient.upload(file).getData().toString();
            if(personMessageMapper.uploadjianli(Id,url)){
                return R.success("上传简历成功");
            }
            return R.success("上传简历失败");
        }
        return R.fail(1,"hh");
    }

    @Override
    public RespResult download() {
            LoginUser loginUser = (LoginUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            String url=personMessageMapper.getjianli(loginUser.getUser().getId());
            return R.success(url);
    }

    @Override
    public RespResult downloadApplication(Integer applicationId) {
        String url=personMessageMapper.getApplication(applicationId);
        return R.success(url);
    }


}
